﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.IO;
using System.Xml;
using System.Threading;
using System.Windows.Threading;

namespace DrawerServer
{
    /// <summary>
    /// Interaction logic for DrawCanvas.xaml
    /// </summary>
    public partial class DrawCanvas : UserControl
    {
        private string visualAdded;
        private int changedIndex;

        public DrawCanvas()
        {
            InitializeComponent();
        }

        public delegate void DrawDelegate();

        public void AddChild(string user, string childAdded, int index)
        {
            if (!string.IsNullOrEmpty(childAdded) && index >= 0)
            {
                this.visualAdded = childAdded;                
                this.changedIndex = index;
                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    new DrawDelegate(AddToCanvas));
                
            }
        }

        public void RemoveChild(string user, string childRemoved, int index)
        {
            if (!string.IsNullOrEmpty(childRemoved) && index >= 0)
            {
                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    new DrawDelegate(RemoveFromCanvas));
            }
        }        

        public void UpdateChild(string user, string childUpdated, int index)
        {
            if (!string.IsNullOrEmpty(childUpdated) && index >= 0)
            {
                this.visualAdded = childUpdated;
                this.changedIndex = index;
                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    new DrawDelegate(UpdateToCanvas));
            }
        }

        private void AddToCanvas()
        {
            if (!string.IsNullOrEmpty(visualAdded))
            {
                using (StringReader stringReader = new StringReader(visualAdded))
                {
                    XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
                    drawCanvas.Children.Insert(changedIndex, XamlReader.Load(xmlTextReader) as UIElement);
                }
            }
        }

        private void UpdateToCanvas()
        {
            if (!string.IsNullOrEmpty(visualAdded))
            {
                using (StringReader stringReader = new StringReader(visualAdded))
                {
                    XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
                    drawCanvas.Children.RemoveAt(changedIndex);
                    drawCanvas.Children.Insert(changedIndex, XamlReader.Load(xmlTextReader) as UIElement);
                }
            }
        }

        public void RemoveFromCanvas()
        {
            drawCanvas.Children.RemoveAt(changedIndex);
        }

        public delegate List<String> _del_GetCanvasElements();

        public List<String> GetCanvasElementList()
        {
            //this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new _del_GetCanvasElements(GetCanvasElements));
            return null;
        }

        public List<String> GetCanvasElements()
        {
            List<String> list = new List<string>();
            foreach (UIElement uie in drawCanvas.Children)
            {
                list.Add(XamlWriter.Save(uie));                
            }
            foreach (String s in list)
            {
                System.Console.WriteLine(s);
            }
            return list;
        }

        private DependencyObject XamlToUIElement(string content)
        {
            using (StringReader stringReader = new StringReader(content))
            {
                XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
                return XamlReader.Load(xmlTextReader) as DependencyObject;
            }
        }        
    }
}